<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="卷名" prop="ex">
        <el-input v-model="queryParams.trainName" placeholder="请输入卷名" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="我的成绩" name="first">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="recordList" ref="dataTable" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="卷名" prop="trainName" />
          <el-table-column label="正确题数" prop="correctNum" width="150" />
          <el-table-column label="错误题数" prop="errorNum" width="100" />
          <el-table-column label="得分" prop="totalScore" width="150" />
          <el-table-column label="合格状态" prop="isPass" width="150">
            <template slot-scope="scope">
              <div>
                <el-tag v-if="scope.row.isPass === -1" type="danger"> 未合格 </el-tag>
                <el-tag v-else type="success"> 合格 </el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="getInfo(scope.row, 2)">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-tab-pane>
      <!-- <el-tab-pane label="模拟成绩" name="second"  @selection-change="handleSelectionChange">
        <el-table v-loading="loading" :data="recordList">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="卷名" prop="trainName" />
          <el-table-column label="正确题数" prop="correctNum" width="150" />
          <el-table-column label="错误题数" prop="errorNum" width="100" />
          <el-table-column label="得分" prop="totalScore" width="150" />

          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
          >
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="getInfo(scope.row,1)"
                >查看</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane> -->
    </el-tabs>
    <RecordInfo ref="RecordInfoRef"></RecordInfo>
  </div>
</template>

<script>
import { getStudentTrainRecordList, getTrainSimulateRecordList, exportStudentTrainRecord } from "@/api/train/index";

import RecordInfo from "./components/record-info.vue";
export default {
  components: {
    RecordInfo,
  },
  data() {
    return {
      activeName: "first",
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainName: "",
        paperType: 1,
      },
      recordList: [],
      selectedRows: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getStudentTrainRecordList(this.queryParams).then((response) => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getSimulateList() {
      getTrainSimulateRecordList(this.queryParams).then(res => {
        if (res.code == 200) {
          this.recordList = res.rows;
          this.total = res.total;
        }
      })
    },

    //切换标签
    handleClick(tab, event) {
      console.log(tab.index);
      if (tab.index == 0) {
        this.getList();
      } else {
        this.getSimulateList();
      }
    },
    getInfo(row, type) {
      console.log(row)
      if (type == 2) {
        this.$refs.RecordInfoRef.show(row.trainRecordId, type);
      } else {
        this.$refs.RecordInfoRef.show(row.trainSimulateRecordId, type);
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.trainName = "";
      this.handleQuery();
    },
    handleSelectionChange(e) {
      console.log(e)
      this.selectedRows = e;
    },
    //导出数据
    handleExport() {
      if (this.selectedRows.length === 0) {
        this.$message.warning('请先选择要导出的数据');
        return;
      }
      //逗号拼接
      let trainRecordIds = this.selectedRows.map(item => item.trainRecordId).join(',');

      this.download('/student/exportStudentTrainRecord', {
        trainRecordIds
      }, `train_${new Date().getTime()}.xlsx`)
      // exportStudentTrainRecord({trainRecordIds:ids}).then(res => { 
      //   // 创建一个 Blob 对象
      //   const blob = new Blob([res], { type: 'application/vnd.ms-excel' });
      //   // 创建一个下载链接
      //   const url = URL.createObjectURL(blob);
      //   const a = document.createElement('a');
      //   a.href = url;
      //   a.download = '导出数据.xlsx'; // 设置下载文件名
      //   document.body.appendChild(a);
      //   a.click(); // 触发下载
      //   document.body.removeChild(a); // 移除链接
      //   URL.revokeObjectURL(url); // 释放 URL 对象
      // })
    },
  },
};
</script>
